function [NP,NT,PSS0,AXIS,SW,GRD,AT] = readprocpar(name);
%
%
%READPROCPAR.m (version 1.3)
%
%Reads in the procpar file for the specified FID
%---------------------
%Usage [NP,NT,PSS0,AXIS,SW,GRD]=readprocpar(name)
%
%Input parameter is name of the FID file whose procpar file is to be read.
%Include the name of FID (without extension) within single quotes.
%
%Other optional parameters that can be specified include (in order):
%
%1. NP      : Number of points in that FID (Required first parameter)
%2. NT      : Number of repetitions
%3. PSS0    : Slice offset (could be an array)
%4. AXIS    : Type of calibration gradient used
%5. SW      : Spectral Width
%6. GRD     : Max. Gradient Strength used (either gpe/gro)
%7. AT      : Acquisition time of each repetition
%-------------------------------
%Example: [NT,PSS0,AXIS]=readprocpar('pinecone_01');
%-------------------------------
%Rick Deshpande [CDFI, University of Alabama at Birmingham]
%
%August 2007 (v1.3) : Added the ability to specify the FID name whose procpar needs to be
%read. This cuts the time needed to change the directory paths each time we
%need to navigate between different directorys
%
%(v1.2) :Made the output more flexible by introducing 'nargout' check which
%determines how many parameters need to be returned
%
%(v1.1) : Bypassed the part arrayed variables were read in. 
%The number of elements in an array are listed in the same
%place where the array elements are listed, which makes reading the
%information about arrayed variables redundant
%
%
if isunix==1    %Check if the program is being run in UNIX or WINDOZE
    fullname = sprintf('%s.fid%cprocpar',name,'/');
else
    fullname = sprintf('%s.fid%cprocpar',name,'\');
end

fid = fopen(fullname,'r','ieee-be');
if fid == -1
    str = sprintf('\n\nError opening the file %s or the file doesnt exist\n\n',fullname);
    error(str);
end

%If the procpar fid exists then load the contents of the procpar file
if isunix==1
    content=fscanf(fopen(strcat(name,'.fid/procpar'),'r'),'%c');
else
    content=fscanf(fopen(strcat(name,'.fid\procpar'),'r'),'%c');
end

if nargout == 0
    display(['No ouput argument specified'])
end

% Initialize the variable values (to empty sets of their particular
% datatype)

NP=double([]);              % No. of acquisition data points
NT=double([]);              % No. of repetitions of a particular sequence (array)
PSS0=double([]);            % Slice offsets (could be arrayed)
AXIS=char([]);              % Gradient used for acquisition (calib = p/n/r)
SW=double([]);              % Spectral width of the system
GRD=double([]);             % Maximum gradient strength utilized
AT=double([]);              % Acquisition time for each sequence

% If output arguments are specified obtain the variable values for each
% output specified. Exit the program once the desired variables are
% obtained.

clc;

%% Revised version

if nargout > 0
    % 1. **Default output: Number of acquired data points

    contentNP=content(findstr(content,'np 7')+6:findstr(content,'nf 1')-5);
    if isempty(contentNP)==1 || max(size(contentNP)) > 180
        contentNP=content(findstr(content,'np 7')+6:findstr(content,'np 7')+90);
        endpt=findstr(contentNP,(char(10)));
        endpt=endpt(2);
        contentNP=contentNP(1:endpt);
        clear endpt
    end
%     contentpss0=str2num(contentpss0(findstr(contentpss0,'64')+3:end));
%     if max(size(contentpss0)) == contentpss0(1)+1
%         PSS0=contentpss0(2:end); % output the slice offset values to pss0
%     end
%     
%      if isempty(contentNP)==1
%          contentNP=content(findstr(content,'np 7')+6:findstr(content,'notebook 2')-5);
%      end
    contentNP=str2num(contentNP(findstr(contentNP,'64')+3:end));
    if max(size(contentNP)) == contentNP(1)+1
        NP=contentNP(2:end)/2; % output the number of acquired points
    end    
end

%Find the location in procpar file where the 'np' varibale is found. The
%variables follow a specific sequence in a procpar file, so we can seek for
%the desired variable followed by the next occuring variable, between which
%the values of a particular varible are found, usually on the second line
%following '64'. In most instances the first number on second line
%specifies how many values are arrayed, which helps in veryfying if we have
%read in the correct number of values for a particular variable.
%This same logic is implement in reading in most of the other parameters
%extracted through this program.

% 2. Extracting the number of repetitions
if nargout > 1
    
    contentNT=content(findstr(content,'nt 7')+6:findstr(content,'nseg 1 1')-5);
    contentNT=str2num(contentNT(findstr(contentNT,'64')+3:end));
    if max(size(contentNT)) == contentNT(1)+1
        NT=contentNT(1); % output the number of repetitions used in this sequence
    end
end

% 3. Extracting slice offsets used, either single or in an array
if nargout > 2
    
    contentpss0=content(findstr(content,'pss0 1')+6:findstr(content,'pslabel 2')-6);
    
    if isempty(contentpss0)==1 || max(size(contentpss0)) > 180
        contentpss0=content(findstr(content,'pss0 1')+6:findstr(content,'pss0 1')+90);
        endpt=findstr(contentpss0,(char(10)));
        endpt=endpt(2);
        contentpss0=contentpss0(1:endpt);
        clear endpt
    end
    contentpss0=str2num(contentpss0(findstr(contentpss0,'64')+3:end));
    if max(size(contentpss0)) == contentpss0(1)+1
        PSS0=contentpss0(2:end); % output the slice offset values to pss0
    end
end

% 4. Which gradient is used for calibration, (calib=p/r/n)
if nargout > 3
    
    contentcalib=content(findstr(content,'calib 2')+6:findstr(content,'cntron 4')-5);
    if isempty(contentcalib)==1 || max(size(contentcalib)) > 100
        contentcalib=content(findstr(content,'calib 2')+6:findstr(content,'calib 2')+90);
        endpt=findstr(contentcalib,(char(10)));
        endpt=endpt(2);
        contentcalib=contentcalib(1:endpt);
        clear endpt
    end
    contentcalib=double(contentcalib(findstr(contentcalib,'64')+3:end));
    AXIS=char(nonzeros(double(ismember(contentcalib,(112)) |...
        ismember(contentcalib,(110)) | ismember(contentcalib,(114))).*contentcalib));

    % 110,112 and 114 are ASCII values for N,P and R respectively. The function
    % 'ismember' sets the bit corresponding to N/P/R to 1 and the remainder of
    % the bits to zeros. This logical array is converted back to a numerical
    % array and multipled with the original array which returns the the type of
    % calibration gradient employed in that acquisition as a numeral. The ouput
    % is then scanned for nonzero components and the existing numeral is
    % converted back to a character.
end

% 5. Read in the spectral width
if nargout > 4
    
    contentSW=content(findstr(content,'sw 1')+6:findstr(content,'studyid_')-5);
    contentSW=str2num(contentSW(findstr(contentSW,'64')+3:end));
    if max(size(contentSW)) == contentSW(1)+1
        SW=contentSW(end); % output the spectral width (determines np)
    end
end

% 6. Max gradient strength used
if nargout > 5
    
    contentGMAX=content(findstr(content,'gpe 1')+6:findstr(content,'gpe3 1')-5);
    contentGMAX=str2num(contentGMAX(findstr(contentGMAX,'64')+3:end));
    if max(size(contentGMAX)) == contentGMAX(1)+1
        GRD=contentGMAX(end); % Gives the maximum gradient strength used
    end
end

% 7. Acquisition time for each acqusition (for each repetition)
if nargout > 6
    
    contentAT=content(findstr(content,'at 1')+6:findstr(content,'awc 1')-5);
    contentAT=str2num(contentAT(findstr(contentAT,'64')+3:end));
    if max(size(contentAT)) == contentAT(1)+1
        AT=contentAT(end); % Acquisition time for each repetition
    end
end

% Usage [NP,NT,PSS0,AXIS,SW,GRD]=read_procpar(procpar)


if nargout > 7
    display(['Too many output arguments'])
end

fclose('all');